package com.master.demo.flowable;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;

import java.util.HashMap;
import java.util.Map;

/**
 * 说明
 *
 * @author: Yang
 * @date: 2021/10/8
 * @version: 1.0.0
 * Copyright Ⓒ 2021 Master Computer Corporation Limited All rights reserved.
 */
public class HolidayRequest {

    public static void main(String[] args) {
        ProcessEngine processEngine = init();
        createDeployment(processEngine);
        startProcess(processEngine);
    }

    /**
     * 初始化flowable引擎并生成数据表
     */
    private static ProcessEngine init() {
        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://192.168.59.134:3306/demo_mall?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false")
                .setJdbcUsername("root")
                .setJdbcPassword("Sipaote2019@")
                .setJdbcDriver("com.mysql.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        ProcessEngine processEngine = cfg.buildProcessEngine();
        return processEngine;
    }

    /**
     * 流程定义部署至flowable引擎
     *
     * @param processEngine
     */
    private static void createDeployment(ProcessEngine processEngine) {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("holiday-request.bpmn20.xml")
                .deploy();

        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId())
                .singleResult();
        System.out.println("Found process definition : " + processDefinition.getName());
    }

    /**
     * 流程定义部署至flowable引擎
     *
     * @param processEngine
     */
    private static void startProcess(ProcessEngine processEngine) {
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("employee", "大师");
        variables.put("nrOfHolidays", 1);
        variables.put("description", "测试");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holidayRequest", variables);
    }


}
